1416B - Make Them Equal - CodeForces Solution


constructive algorithms greedy math *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h> 
using namespace std;
#define ll                long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define nl                "\n"
#define all(x)            (x).begin(),(x).end()
#define sz(x)             (int)((x).size())
#define pii               pair<ll, ll>
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
 
const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;
 
void run_case(){
    int n;
    cin >> n;
    vector<int> a(n + 1);
    int sum = 0;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
        sum += a[i];
    }      
    
    if(sum % n) {
        cout << -1 << nl;
        return;
    }
    
    vector<vector<int>> ops;
    for(int i = 2; i <= n; ++i) {
        if(a[i] == 0)
            continue;
        if(a[i] % i)
            ops.pb({1, i, i - (a[i] % i)});
        ops.pb({i, 1, (a[i] + i - 1) / i});
    }
    
    int val = sum / n;
    
    for(int i = 2; i <= n; ++i) {
        ops.pb({1, i, val});
    }
    
    cout << ops.size() << nl;
    for(auto v: ops) {
        for(auto x : v)
            cout << x << " ";
        cout << nl;
    }
}
 
int main(){

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false); 
    cin.tie(0); cout.tie(0);
    
    int tests;
    cin >> tests;
    while(tests--){
        run_case();
    }

}


Comments

Submit
0 Comments
More Questions

141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number